using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Labirinto_Magico
{
    public class MainMenu : Microsoft.Xna.Framework.DrawableGameComponent
    {
        List<GUIButton> options     = null;
        RenderingData mRenderWrapper = null;
        SoundHandler mSound          = null;
        GUILabel mTitle              = null;

        bool existFile = false;

        public MainMenu(Game game)
            : base(game)
        {
            options     = new List<GUIButton>();
            mRenderWrapper     = (RenderingData)Game.Services.GetService(typeof(RenderingData));
            mSound      = (SoundHandler)Game.Services.GetService(typeof(SoundHandler));
        }

        public override void Initialize()
        {
            int halfWidth = Game.GraphicsDevice.Viewport.Width / 2; 
            int halfHeight = Game.GraphicsDevice.Viewport.Height / 2;
            int widthOnThree = Game.GraphicsDevice.Viewport.Width / 3;
            int heightOnThree = Game.GraphicsDevice.Viewport.Height / 3;
            int heightOnFour = halfHeight / 2;
            int widthOnFour = halfWidth / 2;

            Vector2 buttonRender = new Vector2(widthOnFour, heightOnFour);

            options.Add(new GUIButton("NEW", new Vector2(widthOnThree, halfHeight), buttonRender));
            options.Add(new GUIButton("TUTORIAL", new Vector2(widthOnThree * 2, halfHeight), buttonRender));
            options.Add(new GUIButton("LOAD", new Vector2(widthOnThree, heightOnFour * 3), buttonRender));
            Vector2 exitPosition = new Vector2(Game.GraphicsDevice.Viewport.Width - widthOnFour * 0.6f, Game.GraphicsDevice.Viewport.Height - heightOnFour * 0.6f);
            options.Add(new GUIButton("EXIT", exitPosition, buttonRender));

            if (MediaPlayer.State != MediaState.Playing)
                MediaPlayer.Play(mSound.mTheme);

            Vector2 titleLabel = new Vector2(Game.GraphicsDevice.Viewport.Width, heightOnThree);
            mTitle = new GUILabel("Title", new Vector2(halfWidth, heightOnThree * 0.6f), titleLabel);

            existFile = File.Exists("savegame.sav");

            base.Initialize();   
        }

        public override void Update(GameTime gameTime)
        {
            if (options.ElementAt(0).PressButton())
            {
                mSound.ButtonSound();
                Game.Components.Remove(this);
                Game.Components.Add(new PlayerChoiceMenu(Game));
            }
            else if (options.ElementAt(2).PressButton())
            {
                if (existFile)
                {
                    mSound.ButtonSound();
                    Game.Components.Remove(this);
                    Game.Components.Add(new GameLogic(Game, LOAD_FROM.File, 0));
                }
            }
            else if (options.ElementAt(1).PressButton())
            {
                mSound.ButtonSound();
                Game.Components.Remove(this);
                Game.Components.Add(new Tutorial(Game));
            }
            else if (options.ElementAt(3).PressButton())
            {
                mSound.ButtonSound();
                Game.Exit();
            }
            
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            mRenderWrapper.DrawTexture(mRenderWrapper.mTextureData.title, mTitle.mDimension, mTitle.mPosition, 0, Color.White);

            foreach (GUIButton option in options)
            {
                Texture2D tex = new Texture2D(GraphicsDevice, 32, 32);
                switch (option.mType)
                {
                    case "NEW":
                        if (option.IsMouseOver())
                            tex = mRenderWrapper.mTextureData.newGameSelected;
                        else
                            tex = mRenderWrapper.mTextureData.newGame;
                        break;

                    case "LOAD":
                        if (existFile)
                        {
                            if (option.IsMouseOver())
                                tex = mRenderWrapper.mTextureData.loadGameSelected;
                            else
                                tex = mRenderWrapper.mTextureData.loadGame;
                        }
                        break;

                    case "TUTORIAL":
                        if (option.IsMouseOver())
                            tex = mRenderWrapper.mTextureData.tutorialSelected;
                        else
                            tex = mRenderWrapper.mTextureData.tutorial;
                        break;

                    default:
                        if (option.IsMouseOver())
                            tex = mRenderWrapper.mTextureData.exitSelected;
                        else
                            tex = mRenderWrapper.mTextureData.exit;
                        break;
                }
                mRenderWrapper.DrawTexture(tex, option.mDimension, option.mPosition, 0, Color.White);
            }

            base.Draw(gameTime);
        }
    }
}
